package com.wwtech.epp.ui.activity.friend;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseActivity;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.db.Friend;
import com.wwtech.epp.entity.multi.PersonItem;
import com.wwtech.epp.ui.activity.friend.component.DaggerAddManagerComponent;
import com.wwtech.epp.ui.activity.friend.contract.AddManagerContract;
import com.wwtech.epp.ui.activity.friend.module.AddManagerModule;
import com.wwtech.epp.ui.activity.friend.presenter.AddManagerPresenter;
import com.wwtech.epp.ui.adapter.main.CheckPersonAdapter;
import com.wwtech.epp.ui.adapter.main.GroupMemberCheckMangerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.friend
 * @Description: $description
 * @date 2017/09/14 15:21:18
 */

public class AddManagerActivity extends BaseActivity implements AddManagerContract.View, CheckPersonAdapter.OnItemClickListenerme ,BaseQuickAdapter.RequestLoadMoreListener{
    @Inject
    AddManagerPresenter mPresenter;

    @Inject
    CheckPersonAdapter checkPersonAdapter;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.recycerView_jiagou)
    RecyclerView recycerViewJiagou;
    @BindView(R.id.jiagou)
    RelativeLayout jiagou;
    private GroupMemberCheckMangerAdapter groupMemberCheckMangerAdapter;
    private int total;
    private final int ONE_PAGE_SIZE = 20;
    private int mNextPage = 1;
    private int currentId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_add_manager);
        ButterKnife.bind(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(checkPersonAdapter);
        checkPersonAdapter.setOnItemClick(this);
    }

    @Override
    protected void initData() {
        setTitle("选择负责人");
    }

    @Override
    protected void setupActivityComponent() {
        DaggerAddManagerComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .addManagerModule(new AddManagerModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(AddManagerContract.AddManagerContractPresenter presenter) {
        mPresenter = (AddManagerPresenter) presenter;
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    /**
     * 从网络拉取的联系人的集合处理完的了的数据的回调，会把这些数据直接显示在recycleview上面
     *
     * @param list
     */
    @Override
    public void groupMemberBack(ArrayList<Friend> list) {
        groupMemberCheckMangerAdapter = new GroupMemberCheckMangerAdapter(list);
        groupMemberCheckMangerAdapter.setOnLoadMoreListener(this, recycerViewJiagou);
        groupMemberCheckMangerAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if (getIntent().getStringExtra("position").equals("-1")) {
                    ContainValue.projectManager = groupMemberCheckMangerAdapter.getData().get(i);
                } else {
                    ContainValue.tempManager = groupMemberCheckMangerAdapter.getData().get(i);
                }
                Intent intent = new Intent();
                intent.putExtra("position", getIntent().getStringExtra("position"));
                setResult(20, intent);
                finish();
            }
        });
        recycerViewJiagou.setAdapter(groupMemberCheckMangerAdapter);
    }


    //适配器里的子条目的点击回调
    @Override
    public void onItemClick(PersonItem item) {
        total = 0;
        if (item.type.equals("qunzu")) {
            jiagou.setVisibility(View.VISIBLE);
            if (groupMemberCheckMangerAdapter != null) {
                groupMemberCheckMangerAdapter.setNewData(new ArrayList<Friend>());
            }
            Map<String, String> map = new HashMap<>();
            map.put("pageSize", "1000");
            map.put("pageNum", "1");
            mPresenter.getGroupMember(item.group.getId() + "", map);
        }

        if (item.type.equals("zuzhi")) {
            if (item.node.getId() == -1) {
                Intent intent = new Intent(this, FrameworkActivity.class);
                intent.putExtra("flag", "checkPerson");
                startActivityForResult(intent, 1);
            } else {
                jiagou.setVisibility(View.VISIBLE);
                if (groupMemberCheckMangerAdapter != null) {
                    groupMemberCheckMangerAdapter.setNewData(new ArrayList<Friend>());
                }
                Map<String, String> map = new HashMap<>();
                mNextPage = 1;
                map.put("pageSize", ONE_PAGE_SIZE + "");
                map.put("pageNum", mNextPage + "");
                mPresenter.getNodeMember(item.node.getId() + "", map);
            }

        }
        if (item.type.equals("contact")) {
            if (getIntent().getStringExtra("position").equals("-1")) {
                ContainValue.projectManager = item.freind;
            } else {
                ContainValue.tempManager = item.freind;
            }
            Intent intent = new Intent();
            intent.putExtra("position", getIntent().getStringExtra("position"));
            setResult(20, intent);
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            jiagou.setVisibility(View.VISIBLE);
            if (groupMemberCheckMangerAdapter != null) {
                groupMemberCheckMangerAdapter.setNewData(new ArrayList<Friend>());
            }
            Map<String, String> map = new HashMap<>();
            mNextPage = 1;
            map.put("pageSize", ONE_PAGE_SIZE + "");
            map.put("pageNum", mNextPage + "");
            total = data.getIntExtra("count", 0);
            currentId = data.getIntExtra("nodeId", -1);
            mPresenter.getNodeMember(data.getIntExtra("nodeId", -1) + "", map);
        }
    }

    @Override
    public void onBackPressed() {
        if (jiagou.getVisibility() == View.VISIBLE) {
            jiagou.setVisibility(View.GONE);
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void onLoadMoreRequested() {
        if (groupMemberCheckMangerAdapter.getData().size() < ONE_PAGE_SIZE || groupMemberCheckMangerAdapter.getData().size() >= total) {
            groupMemberCheckMangerAdapter.loadMoreEnd(true);
            return;
        }
        mNextPage ++;
        Map<String, String> map = new HashMap<>();
        map.put("pageSize", ONE_PAGE_SIZE + "");
        map.put("pageNum", mNextPage + "");
        mPresenter.getNodeMember(currentId + "", map);
    }
}